package com.talkyun.utils.loader.impl;

import com.talkyun.utils.loader.utils.HttpUtils;

import java.io.IOException;

public class HttpLoader extends AbstractStore {
    private HttpUtils utils;

    public HttpLoader(String url, int timeout) {
        StringBuilder sb = new StringBuilder(256);
        String[] urls = url.split(";");
        for (String url2 : urls) {
            if (this.isBlank(url2)) {
                continue;
            }
            if (sb.length() != 0) {
                sb.append(";");
            }
            url2 = url2.trim();
            sb.append(url2);
        }
        utils = new HttpUtils(sb.toString(), timeout);
    }

    public String load() throws IOException {
        return doLoad(null);
    }

    public String load(String body) throws IOException {
        return doLoad(body);
    }

    private String doLoad(String body) throws IOException {
        String json = null;
        if (isBlank(body)) {
            json = utils.get();
        } else {
            json = utils.post(body);
        }

        if (isBlank(json) || "null".equalsIgnoreCase(json.trim())) {
            return null;
        }
        return json;
    }
}